<template>
  <div class='Login'>
    <div class='Login-container'>
      <div class='title'>欢迎登陆</div>
      <div class='content'>
        <el-form :model='loginForm' :rules='loginFormrules' ref='loginFormRef'>
          <el-form-item lable prop='user'>
            <i class='iconfont icon-yonghu'></i>
            <el-input placeholder='请输入账号' v-model='loginForm.user'></el-input>
          </el-form-item>
          <el-form-item prop='pwd'>
            <i class='iconfont icon-mima'></i>
            <el-input lable type='number' show-password placeholder='请输入密码' v-model='loginForm.pwd'></el-input>
          </el-form-item>
        </el-form>
        <el-button @click='login'>登录</el-button>
      </div>
      <div class='bottom'>
        <div class='orther'>其他登录方式</div>
        <img src='../assets/img/weixin.png' alt />
      </div>
    </div>
  </div>
</template>

<script>
import { setlogin } from "../api/index";
import {getUserMes} from '../api/index'
export default {
  data() {
    return {
      loginForm: {
        user: "",
        pwd: "",
      },

      loginFormrules: {
        user: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        pwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    async login() {
      // let arr = lodash.cloneDeep(this.loginForm)
      // console.log(typeof this.loginForm.pwd);
      console.log(this.loginForm.user, this.loginForm.pwd);
      const { data: res } = await setlogin(
        this.loginForm.user,
        this.loginForm.pwd
      );
      // console.log(res);
      this.$store.state.userInfo = res;
      if (res.code === 200) {
        window.sessionStorage.setItem("token", res.token);
        window.sessionStorage.setItem(
          "userInfo",
          JSON.stringify(this.$store.state.userInfo)
        );
        // 获取用户详细信息
        const { data: res2 } = await getUserMes(
          this.$store.state.userInfo.account.id
        );
        this.$store.state.userMessage = res2;
        window.sessionStorage.setItem(
          "userMessage",
          JSON.stringify(this.$store.state.userMessage)
        );
        this.$router.push("/user");
      } else {
        this.$message.error("登陆失败");
      }
      this.$refs.loginFormRef.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.Login {
  position: relative;
  width: 100vw;
  height: 100vh;
  background-color: orangered;
  .Login-container {
    position: absolute;
    top: 44%;
    left: 50%;
    width: 6rem;
    height: 7rem;
    // background-color: pink;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    .title {
      padding: 0.2rem;
      color: #fff;
      font-size: 0.6rem;
      font-weight: 600;
      letter-spacing: 0.2em;
      margin-bottom: 0.6rem;
    }
    .content {
      width: 6rem;
      display: flex;
      flex-direction: column;
      align-items: center;
      .el-form-item {
        // position: absolute;
        width: 5rem;
        display: flex;
        align-items: center;
        // justify-content: center;
        .iconfont {
          position: absolute;
          top: 0.1rem;
          left: -0.35rem;
        }
        .el-input {
          width: 5rem;
          height: 0.8rem;
          border: 1px solid #ccc;
          border-radius: 0.5rem;
          overflow: hidden;
          outline: none;
        }
      }
      .el-button {
        width: 5rem;
        border-radius: 0.5rem;
        letter-spacing: 1em;
        color: #ccc;
        margin-top: 0.5rem;
      }
    }
    .bottom {
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-top: 0.5rem;
      img {
        width: 0.6rem;
        height: 0.6rem;
      }
    }
  }
}
</style>